package cqut.wys.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * --------神兽出没--------/
 *   ┌─┐     ┌─┐
 * ┌─┘─┴─────┘─┴─┐
 * │      ─      │
 * │  ┬─┘   └─┬  │
 * │             │
 * │      ┴      │  Code is far away from bug with the animal protecting
 * └───┐      ┌──┘  神兽保佑,代码无bug
 *     │      │
 *     │      └──────┐
 *     │             ├┐
 *     │             ┌┘
 *     └┐ ┐ ┌───┬─┐ ┌┘
 *      │ ┤ ┤   │ ┤ ┤
 *      └─┴─┘   └─┴─┘
 * --------感觉萌萌哒--------/
 *
 * 客户端运行测试
 * @author wuyoushan
 * @date 2018/5/18
 */
public class Client {

    public static void main(String[] args) throws Throwable {
        final HelloService target = new HelloServiceImpl();
        ProxyFactory factory = new JavassistProxyFactory();

        InvocationHandler handler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String methodName = method.getName();
                //打印日记
                System.out.println("[before] The method "+methodName+" begins");
                Object result = null;
                try {
                    //前置通知
                    result = method.invoke(target,args);

                    //返回通知,可以访问到方法的返回值
                    String str = String.format("after method:%s execute",method.getName());
                    System.out.println(str);
                }catch (Exception e){
                    e.printStackTrace();
                    //异常通知，可以访问到方法出现的异常
                }

                //后置通知，因为方法可以能会出异常，所以访问不到方法的返回值
                //打印日记
                System.out.println("[after] the method ends with " + result);
                System.out.println("======================================");
                return result;
            }
        };

        HelloService proxy = factory.getProxy(target,handler);
        System.out.println(proxy);

        proxy.say("www");
        proxy.echo("qqq");
        proxy.getHobbies();
        proxy.insert(new User());
        proxy.getUser();
        proxy.getUser("A",23);
    }
}
